package ac.il.technion.c236369.hw4.core.queues;

import java.net.Socket;

import org.apache.log4j.Logger;

import ac.il.technion.c236369.hw4.core.queues.exception.NotEnoughtPlaceInQueue;

public class InsertSocketQueue implements Runnable {

	private Logger log = Logger.getLogger(InsertSocketQueue.class);
	
	private Socket socket = null;

	public InsertSocketQueue( Socket socket) {
		this.socket = socket;
	}
	
	@Override
	public void run() {
		while( true) {
			try {
				if ( socket != null) {
					SocketQueue.getInstance( ).add( socket);
					if ( log.isDebugEnabled( )) {
						log.debug( "Insert incoming socket into the queue, connection host: <" + socket.getInetAddress( ) + ">");
					}
					return;
				}
			} catch (NotEnoughtPlaceInQueue e) {
				try {
					Thread.sleep( 500);
				} catch (InterruptedException e1) {
					log.error(e);
				}
			}
		}
	}

}
